package com.dkm.controller;

import java.util.List;
import java.util.Optional;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dkm.entity.Module;
import com.dkm.entity.Permission;
import com.dkm.entity.Role;
import com.dkm.service.ModuleService;
import com.dkm.service.PermissionService;
import com.dkm.service.TreeService;
import com.dkm.util.ResultUtil;
import com.dkm.util.TreeNode;

@RestController
@RequestMapping("/permission")
public class PermissionController {
	
	//注入模块业务层
	@Autowired
	private ModuleService moduleService;
	//注入权限业务层
	@Autowired
	private PermissionService permissionService;
	//注入树业务层
	@Autowired
	private TreeService treeService;

	@GetMapping("/getLayuiModuleAndPermissionTree")
	@RequiresPermissions(value = {"module:query","permission:query"})
	public List<TreeNode> getLayuiModuleAndPermissionTree(String role_id) {
		
		//获取全部模块信息
		List<Module> modules=moduleService.findAllModules();
		
		//获取全部权限信息
		List<Permission> permissions=permissionService.getAllPermissions();
		
		//获取模块和权限树
		List<TreeNode> treeNodes=treeService.getLayuiModuleAndPermissionTree(modules, permissions);
		//如果角色id为空，直接返回模块和权限树
		if(role_id==null) {
			return treeNodes;
		}
		//回显角色的模块权限树
		return permissionService.getCheckedModulePermissionTreeByRole(role_id, treeNodes);
	}
	
	//根据权限id获取权限信息
	@GetMapping("/getPermissionById")
	public ResultUtil getPermissionById(String permission_id) {
		Optional<Permission> permission = permissionService.getPermissionById(permission_id);
		if(permission.get()!=null) {
			return new ResultUtil(0, "数据获取成功！", permission.get());
		}
		return new ResultUtil(-1, "数据获取失败", null);
	}
}
